package com.bruce.user.client.controller;

import com.bruce.pojo.User;
import com.bruce.rpc.RpcFactory;
import com.bruce.user.userService.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Bruce
 */
@Controller
public class UserController {
    private UserService userService;

    public UserController(){
        try {
            this.userService = RpcFactory.getServiceProxy(UserService.class);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("服务"+ UserService.class.getName() + "不存在");
        }
    }

    @RequestMapping("/getUsersByName")
    @ResponseBody
    public List<User> getUserByName(String name){
        try {
            System.out.println(name);
            System.out.println(userService.getUserByName(name));
            return userService.getUserByName(name);
        } catch (RemoteException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}
